#include "IO.h"


Digraph *readFile(const char *name, int gui){
	register long int i,j,count;
  	int limits[2] = {0,0}; // limits[0] : Vertices limits[1] : Edges
  	int vertex[3] = {0,0,0}; // vertex[0]: Axis x , vertex[1] : Axis y, vertex[2] : value;
	int length = 0;
  	int get = 0;
  	int indword = 0;
  	int indbit = 0;
  	FILE *pfile;
   	vEBTreeFactory<WORDTYPE>* factory = new ArrayvEBTreeFactory<WORDTYPE>(); 
  	int wordz = 0;
  	int numVertices = 0;
  	vEBTree<WORDTYPE>** graph;
  	Digraph *digraph;

  	char *string = (char*) malloc(sizeof(char)*100);

   	if((pfile = fopen(name,"r"))==NULL)
  	{
      		printf("Error: There aren't this file.");
      		getchar();
      		return NULL;
  	}
  	//if(gui)printf("\n\t\tREADING FILE...\n");
  	//if(gui)printf("Commentations:\n");
  	//if(gui)printf("\t*****************************************************************\n");
  	while(!feof(pfile))
  	{
    		get = fgetc(pfile);
    		switch(get)
    		{
      			case 'c':
        			count = -1;
        			while(get != (int)(0xA))
       			 	{
          				count++;
          				get = fgetc(pfile);
         				string[count] = get;
       				 }
       				string[++count] = '\0';
        			if(gui)printf("%s",string);
        			break;

      			case 'p':
        			count = -1;
        			while(get != (int)(0xA))
        			{
          				count++;
          				get = fgetc(pfile);
          				string[count] = get;
        			}
        			string[++count] = '\0';
        			j = 1;
        			for(i = count-1; i > -1 ; i--)
        			{
          				if( (string[i] == ' ') && (isdigit(string[i+1])) && (j>-1) )
          				{
            					string[i]= '\0';
            					limits[j--]= atoi(&string[i+1]);
          				}
        			}

        			numVertices = limits[0];

        			wordz = numVertices/bound;

        			wordz += (numVertices%bound==0)?0:1;

        			graph = (vEBTree<WORDTYPE>**) malloc(sizeof(vEBTree<WORDTYPE>*)*numVertices);


        			for( i = 0; i < numVertices; i++){
           				 graph[i] = new ArrayvEBTree<WORDTYPE>(factory, numVertices, 1);
        			}

        			//if(gui)printf("\nNumber of Vertices %d, Number of length %d\n\n",limits[0],wordz);
        			//if(gui)printf("\t*****************************************************************\n");

        			break;

      			case 'e':
       				 {

          				count = -1;

          				while(get != (int)(0xA))
          				{
            					count++;
            					get = fgetc(pfile);
            					string[count] = get;
          				}

          				string[++count] = '\0';
          				j = 1;

          				for(i = count-1; i > -1 ; i--)
          				{
           					 if( (string[i] == ' ') && (isdigit(string[i+1])) && (j>-1) )
            					{
            				 		string[i]= '\0';
              						vertex[j--]= atoi(&string[i+1]);
            					}
          				}

        			}

       			 	indword = 0;
        			indword = vertex[1]/(bound);
        			indbit = vertex[1]%(bound);


        			graph[vertex[0]-1]->toSet(vertex[1]-1);
        			//if(graph[vertex[0]]->isSet(vertex[1]))if(gui)printf("Adicionado aresta: e(%d, %d), l:%d w:%d, 					bit:%d\n",vertex[0],vertex[1],vertex[0],indword,indbit);
        			indword = 0;
        			indbit = 0;
        			break;
    			}
  	}
    	digraph = (Digraph*) malloc(sizeof(Digraph));
    	digraph->adj = graph;
    	digraph->size = numVertices;
   	//if(gui)printf("\n\tCOMPLETE READING!\n");
    	//if(gui)printf("\n\t*****************************************************************\n");
    	fclose(pfile);
	printf("\n\tCOMPLETE READING!\n");
    	return digraph;
}


int printFile(Digraph *digraph, char *name, int nvertices ,int gui)
{
  if(gui)printf("\tPrint Digraph on file: %s...\n",name);
  int i,j,k;
  FILE *pfile = NULL;
  vEBTree<WORDTYPE>** graph = digraph->adj;
  const int size = digraph->size;

  if((pfile = fopen(name,"w+"))==NULL)
  {
    if(gui)printf("ERROR, THE SYSTEM DID CAN'T CREATE A FILE!,SORRY!");
    return 0;
  }

  if(gui)printf("\n\n\t\t\t\tREADING FILE");
  if(gui)printf("\n\t => Digraph: size = %d",size);
  if(gui)printf("\n\t*****************************************************************\n");

  if(gui)printf("\n\tPROCESSING...\n");

  fprintf(pfile,"c ParGO Rearch Group - UFC\n");
  fprintf(pfile,"c File name = %s\nc\n",name);
  fprintf(pfile,"c Environment:\n");
  if(nvertices) fprintf(pfile,"p %d %d\n",nvertices,0);


    for( i = 0 ; i < size; i++){
        for( j = 0 ; j < size; j++){
        	if(graph[i]->isInSet(j)){
                    if(gui)printf("e %d %d \n", i , j);
                    fprintf(pfile,"e %d %d\n", i , j );
               }
        }
    }


    if(gui)printf("\n\tCOMPLETE PRINTING!\n");
    if(gui)printf("\n\t*****************************************************************\n");

    if(gui)printf("\tFinished Read");

    fclose(pfile);
    return 1;
}


